package com.example.common.config;

import com.alibaba.dashscope.aigc.generation.Generation;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通义千问配置类
 */
@Configuration
public class DashScopeConfig {

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    /**
     * 在Spring容器初始化后立即设置API密钥
     */
    @PostConstruct
    public void init() {
        // 设置API密钥到系统属性，供DashScope SDK全局使用
        System.setProperty("dashscope.api.key", apiKey);
        System.out.println("✅ DashScope API Key 已配置: " + 
            (apiKey != null && apiKey.startsWith("sk-") ? "YES" : "NO"));
    }

    /**
     * 配置通义千问Generation Bean
     */
    @Bean
    public Generation generation() {
        return new Generation();
    }
}

